package com.hzy.ums.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.ums.domain.bo.MemberAddressBo;
import com.hzy.ums.domain.vo.MemberAddressVo;
import com.hzy.ums.domain.MemberAddress;
import com.hzy.ums.mapper.MemberAddressMapper;
import com.hzy.ums.service.IMemberAddressService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员收货地址Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-19
 */
@RequiredArgsConstructor
@Service
public class MemberAddressServiceImpl implements IMemberAddressService {

    private final MemberAddressMapper baseMapper;

    /**
     * 查询会员收货地址
     */
    @Override
    public MemberAddressVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询会员收货地址列表
     */
    @Override
    public TableDataInfo<MemberAddressVo> queryPageList(MemberAddressBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MemberAddress> lqw = buildQueryWrapper(bo);
        Page<MemberAddressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员收货地址列表
     */
    @Override
    public List<MemberAddressVo> queryList(MemberAddressBo bo) {
        LambdaQueryWrapper<MemberAddress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MemberAddress> buildQueryWrapper(MemberAddressBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MemberAddress> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMemberId() != null, MemberAddress::getMemberId, bo.getMemberId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), MemberAddress::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneHidden()), MemberAddress::getPhoneHidden, bo.getPhoneHidden());
        lqw.eq(bo.getDefaultStatus() != null, MemberAddress::getDefaultStatus, bo.getDefaultStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPostCode()), MemberAddress::getPostCode, bo.getPostCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), MemberAddress::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), MemberAddress::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), MemberAddress::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailAddress()), MemberAddress::getDetailAddress, bo.getDetailAddress());
        lqw.eq(bo.getIsDefault() != null, MemberAddress::getIsDefault, bo.getIsDefault());
        lqw.eq(bo.getProvinceId() != null, MemberAddress::getProvinceId, bo.getProvinceId());
        lqw.eq(bo.getCityId() != null, MemberAddress::getCityId, bo.getCityId());
        lqw.eq(bo.getDistrictId() != null, MemberAddress::getDistrictId, bo.getDistrictId());
        return lqw;
    }

    /**
     * 新增会员收货地址
     */
    @Override
    public Boolean insertByBo(MemberAddressBo bo) {
        MemberAddress add = BeanUtil.toBean(bo, MemberAddress.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会员收货地址
     */
    @Override
    public Boolean updateByBo(MemberAddressBo bo) {
        MemberAddress update = BeanUtil.toBean(bo, MemberAddress.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MemberAddress entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员收货地址
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
